package com.lune.job.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lune.job.pojo.Certificate;
import com.lune.job.pojo.ResPageBean;
import com.lune.job.pojo.RespBean;
import com.lune.job.pojo.Resume;
import com.lune.job.service.ICertificateService;
import com.lune.job.service.IResumeService;
import com.lune.job.utils.UserThreadLocal;
import com.lune.job.vo.UserData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Caiyilin
 * @since 2022-03-06
 */
@RestController
@RequestMapping("/certificate")
@Api(tags = "证书管理")
public class CertificateController {

    @Autowired
    private ICertificateService certificateService;
    @Autowired
    private IResumeService resumeService;

    @PostMapping("/")
    @ApiOperation(value = "添加证书")
    public RespBean create(@RequestBody Certificate certificate){
        boolean flag = certificateService.save(certificate);
        if (flag){
            return RespBean.success("添加证书成功");
        }else{
            return RespBean.error("添加证书失败");
        }

    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除证书")
    public RespBean delete(@PathVariable Integer id){
        if (certificateService.removeById(id)){
            return RespBean.success("删除证书成功");
        }else {
            return RespBean.error("删除证书失败");
        }
    }

    @DeleteMapping("/")
    @ApiOperation(value = "批量删除证书")
    public RespBean deleteByIds(Integer[]  ids){
        if (certificateService.removeByIds(Arrays.asList(ids))){
            return RespBean.success("批量删除证书成功");
        }else {
            return RespBean.error("批量删除证书失败");
        }
    }

    @PutMapping("/")
    @ApiOperation(value = "修改证书")
    public RespBean update(@RequestBody Certificate certificate){
        if (certificateService.updateById(certificate)){
            return RespBean.success("修改证书成功");
        }else {
            return RespBean.error("修改证书失败");
        }
    }

    @GetMapping("/detail")
    @ApiOperation(value = "根据id查询证书")
    public RespBean detail(Integer id){
        Certificate byId = certificateService.getById(id);
        if (byId!=null){
            return RespBean.success("查询成功",byId);
        }else {
            return RespBean.error("查询失败");
        }
    }

    @GetMapping("/queryPage")
    @ApiOperation(value = "分页查询证书")
    public ResPageBean queryPage(@RequestParam(defaultValue = "1") Integer currentPage,
                                 @RequestParam(defaultValue = "10") Integer size,
                                 Certificate certificate){
        IPage<Certificate> certificateIPage = certificateService.queryPage(currentPage, size, certificate);
        certificateIPage.getRecords().forEach(item->{
            Resume byId = resumeService.getById(item.getResume_id());
            item.setResume(byId);
        });
        return new ResPageBean(certificateIPage.getTotal(),certificateIPage.getPages(),certificateIPage.getRecords());
    }

    @PostMapping("/queryByCon")
    @ApiOperation(value = "条件查询证书")
    public List<Certificate> queryByCon(@RequestBody Certificate certificate){
        return certificateService.queryByCon(certificate);
    }



}
